import { h } from "vue";
import * as Icons from "@vicons/tabler";
import {NIcon} from "naive-ui";
import type {Component} from "vue";


export const IconMap: Map<string, Component> = new Map();


/**
 * 驼峰转短横线
 * @param name
 */
export const pascalToCamel = (name: string) => {
    return name.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
}


// 加载所有图标
Object.entries(Icons).forEach(([key, component]) => {
    IconMap.set(pascalToCamel(key), component);
})


/**
 * 渲染图标
 * @param name
 */
export const renderIcon = (name: string) => {
    const Icon = IconMap.get(pascalToCamel(name));
    return h(NIcon, null, {default: () => h(Icon as Component)});
}
